require('../less/perData.less')
document.ready(function () {
    // 获取元素节点
    let returnBtn = document.getElementById('returnBtn') //返回上一级
    let closeBtn = document.querySelector('.closeBtn')
    let qvater = document.getElementById('qvater') //头像
    let showdataPicker = document.getElementById('showdataPicker') //文件上传函数
    let sex = document.getElementById('sex') //性别
    let chooseSex = document.getElementById('chooseSex') //选择性别
    let DatePicker = document.getElementById('DatePicker') //生日选项框
    let showBir = document.getElementById('showBir') //生日选项框
    let provincePicker = document.getElementById('provincePicker') //省份选择器
    let province = document.getElementById('province') //省份选择器
    let cityPicker = document.getElementById('cityPicker') //省份选择器
    let city = document.getElementById('city') //省份选择器
    let saveInfo = document.getElementById('saveInfo') //保存信息
    let nickName = document.getElementById('nickName') //昵称
    //点击返回上一级
    returnBtn.addEventListener('click', function () {
        window.history.go(-1)
    })
   
    // let userData = JSON.parse(localStorage.getItem('userData'))
    // console.log(userData);
    let imgurl;
    // 上传头像
    showdataPicker.addEventListener('change', function () {
        // console.dir(this.files[0])
        let file = this.files[0]
        window.$uploadImg('/users/upload', 'imgurl', file, function (res) {
            if (res.status == 0) {
                qvater.src = 'http://139.9.177.51:8099' + res.data
                imgurl = res.data
            } else if (res.status == 1) {
                alert('格式不正确');
            }

        })

    })
    // console.log(imgurl);
    // 点击头像上传文件
    qvater.addEventListener('click', function () {
        showdataPicker.click()
    })

    // 选择性别
    chooseSex.addEventListener('click', function () {
            weui.picker([{
                label: '男',
                value: 0
            }, {
                label: '女',
                value: 1
            }, {
                label: '就不告诉你',
                value: 2
            }], {
                onConfirm: function (result) {
                    // console.log(result);
                    sex.innerHTML = result[0].label
                },
                title: '选择性别'
            });

        }

    )

    let birthday = ''; //定义变量保存生日
    //选择生日
    DatePicker.addEventListener('click', function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                let arr = result.map(function (v, i) {
                    return window.utils.zeroNum(v.value)
                }).join('-');
                showBir.textContent =arr;
                 birthday =arr;
                // console.log(birthday);
            },
            title: '请选择生日'
        })

    })

    // 省份
    let provinceData; //定义变量保存数据
    let cityData; //定义变量保存value
    window.$http.get('/address/province', function (res) {
        // console.log(res);
        let arr = res.data.map(function (v) {
            // console.log(v);
            return {
                label: v.name,
                value: v.addressId
            }
        })
        provinceData = arr

    })

    provincePicker.addEventListener('click', function () {
        if (!provinceData) return
        weui.picker(provinceData, {
            onConfirm: function (result) {
                province.textContent = result[0].label
                //   console.log(result);
                cityData = result[0].value
            },
            title: '请选择省份'
        });

    })
    // 点击获取城市
    cityPicker.addEventListener('click', function () {
        if (!cityData) return
        window.$http.get(`/address/city/${cityData}`, function (res) {
            let arr = res.data.map(function (v) {
                return {
                    label: v.name,
                    value: v.addressId
                }
            })
            // console.log(arr);
            weui.picker(arr, {
                onConfirm: function (result) {
                    city.textContent = result[0].label
                    //   console.log(result);

                },
                title: '请选择城市'
            });
        })
        // console.log(cityData);
    })

    //保存数据
    let id = JSON.parse(window.localStorage.getItem('userData')).userId;
    //点击保存信息,将信息提交到服务器
    saveInfo.addEventListener('click', function () {
        // console.log(imgurl);
        //    发送用户的信息到服务器
        window.$http.post('/users/userEdit', {
            userId: id,
            imgurl: imgurl,
            nickname: nickName.value,
            birthday:birthday,
            address:[province.textContent, city.textContent],   //省份和城市数据
            sex: sex.innerHTML,
        }, function (res) {
            // console.log(res);
            if (res.status == 0) {
                alert('修改成功')
                window.location.href = './mine.html'
            } else {
                alert('修改失败')
            }
        })
    })
    // 把我的里面的信息存到本地存储
    let data = JSON.parse(window.localStorage.getItem('userData'))
    qvater.src = 'http://139.9.177.51:8099' + data.imgurl //本地头像
    nickName.value = data.nickname //昵称
    showBir.textContent=data.birthday.split('T')[0]  //生日
    // console.log(data.birthday.split('T')[0] );
    // console.log(showBir);
    province.textContent=data.address.split(',')[0]  //省份
    city.textContent=data.address.split(',')[1]   //城市
    // console.log(data);
    //回填头像
    if (data.imgurl) {
        qvater.src = 'http://139.9.177.51:8099' + data.imgurl
    }
   







})